import { Goodss } from './goods';
let _Goodss = Goodss;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import GoodsMock from './mallm/goods/goodsMock';
 * GoodsMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/goods/goods/listKey').reply(config => {
      let params = config.params;
      let mockGoodss = _Goodss.filter(goods => {
      	if(params.id||params.id==''){ if( goods.id.indexOf(params.id)>=0) return true;}
      	if(params.categoryId||params.categoryId==''){ if( goods.categoryId.indexOf(params.categoryId)>=0) return true;}
      	if(params.goodsSn||params.goodsSn==''){ if( goods.goodsSn.indexOf(params.goodsSn)>=0) return true;}
      	if(params.name||params.name==''){ if( goods.name.indexOf(params.name)>=0) return true;}
      	if(params.brandId||params.brandId==''){ if( goods.brandId.indexOf(params.brandId)>=0) return true;}
      	if(params.goodsNumber||params.goodsNumber==''){ if( goods.goodsNumber.indexOf(params.goodsNumber)>=0) return true;}
      	if(params.keywords||params.keywords==''){ if( goods.keywords.indexOf(params.keywords)>=0) return true;}
      	if(params.goodsBrief||params.goodsBrief==''){ if( goods.goodsBrief.indexOf(params.goodsBrief)>=0) return true;}
      	if(params.goodsDesc||params.goodsDesc==''){ if( goods.goodsDesc.indexOf(params.goodsDesc)>=0) return true;}
      	if(params.isOnSale||params.isOnSale==''){ if( goods.isOnSale.indexOf(params.isOnSale)>=0) return true;}
      	if(params.addTime||params.addTime==''){ if( goods.addTime.indexOf(params.addTime)>=0) return true;}
      	if(params.sortOrder||params.sortOrder==''){ if( goods.sortOrder.indexOf(params.sortOrder)>=0) return true;}
      	if(params.isDelete||params.isDelete==''){ if( goods.isDelete.indexOf(params.isDelete)>=0) return true;}
      	if(params.attributeCategory||params.attributeCategory==''){ if( goods.attributeCategory.indexOf(params.attributeCategory)>=0) return true;}
      	if(params.counterPrice||params.counterPrice==''){ if( goods.counterPrice.indexOf(params.counterPrice)>=0) return true;}
      	if(params.extraPrice||params.extraPrice==''){ if( goods.extraPrice.indexOf(params.extraPrice)>=0) return true;}
      	if(params.isNew||params.isNew==''){ if( goods.isNew.indexOf(params.isNew)>=0) return true;}
      	if(params.goodsUnit||params.goodsUnit==''){ if( goods.goodsUnit.indexOf(params.goodsUnit)>=0) return true;}
      	if(params.primaryPicUrl||params.primaryPicUrl==''){ if( goods.primaryPicUrl.indexOf(params.primaryPicUrl)>=0) return true;}
      	if(params.listPicUrl||params.listPicUrl==''){ if( goods.listPicUrl.indexOf(params.listPicUrl)>=0) return true;}
      	if(params.retailPrice||params.retailPrice==''){ if( goods.retailPrice.indexOf(params.retailPrice)>=0) return true;}
      	if(params.sellVolume||params.sellVolume==''){ if( goods.sellVolume.indexOf(params.sellVolume)>=0) return true;}
      	if(params.primaryProductId||params.primaryProductId==''){ if( goods.primaryProductId.indexOf(params.primaryProductId)>=0) return true;}
      	if(params.unitPrice||params.unitPrice==''){ if( goods.unitPrice.indexOf(params.unitPrice)>=0) return true;}
      	if(params.promotionDesc||params.promotionDesc==''){ if( goods.promotionDesc.indexOf(params.promotionDesc)>=0) return true;}
      	if(params.promotionTag||params.promotionTag==''){ if( goods.promotionTag.indexOf(params.promotionTag)>=0) return true;}
      	if(params.appExclusivePrice||params.appExclusivePrice==''){ if( goods.appExclusivePrice.indexOf(params.appExclusivePrice)>=0) return true;}
      	if(params.isAppExclusive||params.isAppExclusive==''){ if( goods.isAppExclusive.indexOf(params.isAppExclusive)>=0) return true;}
      	if(params.isLimited||params.isLimited==''){ if( goods.isLimited.indexOf(params.isLimited)>=0) return true;}
      	if(params.isHot||params.isHot==''){ if( goods.isHot.indexOf(params.isHot)>=0) return true;}
      	if(params.wholesalePrice||params.wholesalePrice==''){ if( goods.wholesalePrice.indexOf(params.wholesalePrice)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( goods.deptid.indexOf(params.deptid)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockGoodss
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/goods/goods/list').reply(config => {
      let params = config.params;
      let mockGoodss = _Goodss.filter(goods => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(goods.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.categoryId||params.categoryId==''){if(goods.categoryId.indexOf(params.categoryId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.goodsSn||params.goodsSn==''){if(goods.goodsSn.indexOf(params.goodsSn)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.name||params.name==''){if(goods.name.indexOf(params.name)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.brandId||params.brandId==''){if(goods.brandId.indexOf(params.brandId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.goodsNumber||params.goodsNumber==''){if(goods.goodsNumber.indexOf(params.goodsNumber)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.keywords||params.keywords==''){if(goods.keywords.indexOf(params.keywords)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.goodsBrief||params.goodsBrief==''){if(goods.goodsBrief.indexOf(params.goodsBrief)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.goodsDesc||params.goodsDesc==''){if(goods.goodsDesc.indexOf(params.goodsDesc)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isOnSale||params.isOnSale==''){if(goods.isOnSale.indexOf(params.isOnSale)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.addTime||params.addTime==''){if(goods.addTime.indexOf(params.addTime)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.sortOrder||params.sortOrder==''){if(goods.sortOrder.indexOf(params.sortOrder)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isDelete||params.isDelete==''){if(goods.isDelete.indexOf(params.isDelete)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.attributeCategory||params.attributeCategory==''){if(goods.attributeCategory.indexOf(params.attributeCategory)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.counterPrice||params.counterPrice==''){if(goods.counterPrice.indexOf(params.counterPrice)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.extraPrice||params.extraPrice==''){if(goods.extraPrice.indexOf(params.extraPrice)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isNew||params.isNew==''){if(goods.isNew.indexOf(params.isNew)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.goodsUnit||params.goodsUnit==''){if(goods.goodsUnit.indexOf(params.goodsUnit)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.primaryPicUrl||params.primaryPicUrl==''){if(goods.primaryPicUrl.indexOf(params.primaryPicUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.listPicUrl||params.listPicUrl==''){if(goods.listPicUrl.indexOf(params.listPicUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.retailPrice||params.retailPrice==''){if(goods.retailPrice.indexOf(params.retailPrice)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.sellVolume||params.sellVolume==''){if(goods.sellVolume.indexOf(params.sellVolume)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.primaryProductId||params.primaryProductId==''){if(goods.primaryProductId.indexOf(params.primaryProductId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.unitPrice||params.unitPrice==''){if(goods.unitPrice.indexOf(params.unitPrice)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.promotionDesc||params.promotionDesc==''){if(goods.promotionDesc.indexOf(params.promotionDesc)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.promotionTag||params.promotionTag==''){if(goods.promotionTag.indexOf(params.promotionTag)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.appExclusivePrice||params.appExclusivePrice==''){if(goods.appExclusivePrice.indexOf(params.appExclusivePrice)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isAppExclusive||params.isAppExclusive==''){if(goods.isAppExclusive.indexOf(params.isAppExclusive)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isLimited||params.isLimited==''){if(goods.isLimited.indexOf(params.isLimited)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isHot||params.isHot==''){if(goods.isHot.indexOf(params.isHot)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.wholesalePrice||params.wholesalePrice==''){if(goods.wholesalePrice.indexOf(params.wholesalePrice)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(goods.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockGoodss
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockGoodss.length;
      mockGoodss = mockGoodss.filter((goods, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockGoodss,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/goods/goods/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _Goodss = _Goodss.filter(goods => goods.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/goods/goods/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _Goodss = _Goodss.filter(goods => !params.includes(goods.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/goods/goods/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,categoryId,goodsSn,name,brandId,goodsNumber,keywords,goodsBrief,goodsDesc,isOnSale,addTime,sortOrder,isDelete,attributeCategory,counterPrice,extraPrice,isNew,goodsUnit,primaryPicUrl,listPicUrl,retailPrice,sellVolume,primaryProductId,unitPrice,promotionDesc,promotionTag,appExclusivePrice,isAppExclusive,isLimited,isHot,wholesalePrice,deptid } = params;
      _Goodss.some(goods => {
        if (goods.id === id) {
			goods.id = id;
			goods.categoryId = categoryId;
			goods.goodsSn = goodsSn;
			goods.name = name;
			goods.brandId = brandId;
			goods.goodsNumber = goodsNumber;
			goods.keywords = keywords;
			goods.goodsBrief = goodsBrief;
			goods.goodsDesc = goodsDesc;
			goods.isOnSale = isOnSale;
			goods.addTime = addTime;
			goods.sortOrder = sortOrder;
			goods.isDelete = isDelete;
			goods.attributeCategory = attributeCategory;
			goods.counterPrice = counterPrice;
			goods.extraPrice = extraPrice;
			goods.isNew = isNew;
			goods.goodsUnit = goodsUnit;
			goods.primaryPicUrl = primaryPicUrl;
			goods.listPicUrl = listPicUrl;
			goods.retailPrice = retailPrice;
			goods.sellVolume = sellVolume;
			goods.primaryProductId = primaryProductId;
			goods.unitPrice = unitPrice;
			goods.promotionDesc = promotionDesc;
			goods.promotionTag = promotionTag;
			goods.appExclusivePrice = appExclusivePrice;
			goods.isAppExclusive = isAppExclusive;
			goods.isLimited = isLimited;
			goods.isHot = isHot;
			goods.wholesalePrice = wholesalePrice;
			goods.deptid = deptid
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/goods/goods/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,categoryId,goodsSn,name,brandId,goodsNumber,keywords,goodsBrief,goodsDesc,isOnSale,addTime,sortOrder,isDelete,attributeCategory,counterPrice,extraPrice,isNew,goodsUnit,primaryPicUrl,listPicUrl,retailPrice,sellVolume,primaryProductId,unitPrice,promotionDesc,promotionTag,appExclusivePrice,isAppExclusive,isLimited,isHot,wholesalePrice,deptid } = params;
      _Goodss.push({
		id:id,
		categoryId:categoryId,
		goodsSn:goodsSn,
		name:name,
		brandId:brandId,
		goodsNumber:goodsNumber,
		keywords:keywords,
		goodsBrief:goodsBrief,
		goodsDesc:goodsDesc,
		isOnSale:isOnSale,
		addTime:addTime,
		sortOrder:sortOrder,
		isDelete:isDelete,
		attributeCategory:attributeCategory,
		counterPrice:counterPrice,
		extraPrice:extraPrice,
		isNew:isNew,
		goodsUnit:goodsUnit,
		primaryPicUrl:primaryPicUrl,
		listPicUrl:listPicUrl,
		retailPrice:retailPrice,
		sellVolume:sellVolume,
		primaryProductId:primaryProductId,
		unitPrice:unitPrice,
		promotionDesc:promotionDesc,
		promotionTag:promotionTag,
		appExclusivePrice:appExclusivePrice,
		isAppExclusive:isAppExclusive,
		isLimited:isLimited,
		isHot:isHot,
		wholesalePrice:wholesalePrice,
		deptid:deptid
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};